package actions
{
	import core.actor.core.ActorView;
	import core.actor.core.BoneData;
	
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import loader.LoadProxy;
	import loader.core.LoadType;

	public class WalkAction extends BaseAction
	{
		private var _target:Point = new Point();
		private var _from:Point = new Point();
		private var _startTime:int;
		private var _duration:int = 2000;
		public function WalkAction()
		{
		}
		
		public override function update(step:int):void
		{
			super.update(step);
			if(getTimer()-_startTime>=_duration)
			{
				_actor.x = _target.x;
				_actor.y = _target.y;
				this.dispatchEvent(new Event(ACTION_COMPLETE));
				super.stop();
				
			}
			else
			//if(_target.x-_actor.x>1
				//&&_target.y-_actor.y>1)
			{
				setLinerTrack(_from,_target,_duration,_startTime);
			}
			
			
		}
		
		protected override function setAni(charType:String):void
		{
			//var a:Array = [];
			
			//var speedx:Number = 3;
			
			/*for(var i:int = 0;i<49;i++)
			{
				var d:BoneData = new BoneData();
				//d.x = (a[i-1][0] as BoneData).x+speedx;
				d.y = 50+5*Math.sin(i);
				//d.scaleX = d.scaleY = Math.random();
				//d.scaleX = d.scaleY  = (a[i-1][0] as BoneData).scaleX; 
				//d.alpha = Math.random();
				//d.rotation = 360*Math.random();
				//d.rotation = (a[i-1][0] as BoneData).rotation;
				d.name = "head";
				a[i] = [d];
			}*/

			_actor.initAni("E:/NewGame/ClientRes/resource/images/ani/100001000/run.ani");
		}
		
		
		public override function init(actor:ActorView):void
		{
			super.init(actor);
			_target.x = actor.x;
			_target.y = actor.y;
		}
		
		public function setPropAndPlay(target:Point,duration:int):void
		{
			_target = target;
			_from.x = _actor.x;
			_from.y = _actor.y;
			_startTime = getTimer();
			_duration = duration;
			super.play();
		}
		
		private function setLinerTrack(start:Point,end:Point,duration:int,startTime:int):void
		{
			var t:int = getTimer() - startTime;
			var rate:Number = t/duration;
			var x:Number = start.x;
			var y:Number = start.y;
			var subX:Number = end.x-start.x;
			var subY:Number = end.y-start.y;
		
			//if(((subX<0)?-subX:subX)>1)
				x = rate*(end.x - start.x)+start.x;
			//if(((subY<0)?-subY:subY)>1)
				y = rate*(end.y - start.y)+start.y;
			_actor.x = x;
			_actor.y = y;
		}
		
	}
}